package cn.rlfit.common.validators;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

/**
 * @author ruolifeng [1877751412@qq.com]
 * @date 2023/2/26 21:06
 */

public class ListValueConstraintValidator implements ConstraintValidator<ListValue, Integer> {
    //    初始化方法
    private Set<Integer> set = new HashSet<>();

    @Override
    public void initialize(ListValue constraintAnnotation) {
        int[] vals = constraintAnnotation.vals();
        for (int v : vals
        ) {
            set.add(v);
        }
    }

    /**
     *
     * @param value object to validate
     * @param context context in which the constraint is evaluated
     *
     * @return
     */
    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        return set.contains(value);
    }
}
